<?php
/**
* This is part of appEngine, a framework kit embedded in webappkit
*
* @link http://webappkit.net
* @package appEngine
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* generic interface for code calls that can be triggered by routes
* currently supports script,function
* @author J.Ducastel <jeremie@ducastel.name>
* @uses httpRequestBean
*/
class wakEvent extends dataBean {
/*--------------------------------------------------------------------
CONSTRUCTOR
--------------------------------------------------------------------*/
	/**
	* constructor alias
	* @access public
	*/
	function wakEvent(&$kit,$id) {
		$this->__construct($kit,$id);
	}/**/
	
	/**
	* "real" constructor
	* @access public
	* @param webappkit $kit parent's kit reference
	* @param string $id event's id
	*/
	function __construct(&$kit,$id) {
		$this->kit=&$kit;
		$this->id=$id;
		// defining type upon id / existing files
		// base path
		$path=$kit->getPath().'events/'; //echo "<p>$path : $id</p>";
		// php name regex
		$name='[a-z][a-z0-9_]*';
		switch (true) {
			// static method
			case ereg("($name)::($name)",$id,$search):
				$this->type='static_method';
				$this->event_class=$search[1];
				$this->event_function=$search[2];
				$this->file=$path.$class.'.class.php';
				break;
			// function
			case file_exists($path.$id.'.function.php'):
				$this->type='function';
				$this->file=$path.$id.'.function.php';
				$this->event_function='event_'.$id;
				break;
			// script
			case file_exists($path.$id.'.script.php'):
				$this->type='script';
				$this->file=$path.$id.'.script.php';
				break;
			default:
				$this->is_valid=false;
				//trigger_error("invalid event id $id");
		}
	}/**/
/*--------------------------------------------------------------------
STATIC METHODS
--------------------------------------------------------------------*/
	/**
	 * gets an event from a kit by id
	 * @static
	 * @param webappkit $kit event's parent kit
	 * @param string $id event's id
	 * @return wakEvent
	 */
	 function & getFromKitById(&$kit,$id) {
		$evt=&new wakEvent($kit,$id);
		if ($evt->isValid())
			return $evt;
		// not valid
		$n=null; return $n;
	 }/**/
/*--------------------------------------------------------------------
PUBLIC INSTANCE METHODS
--------------------------------------------------------------------*/
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* executes event
	* @param httpRequestBean $request
	* @access public
	* @return mixed
	*/
	function run(&$request) {
		switch ($this->type) {
			case 'script':
				return $this->_runScript($request);
			case 'function':
				return $this->_runFunction($request);
			default:
				return null;
		}
	}/**/
	
	/**
	 * is a valid event
	 * @return bool
	 */
	function isvalid() {
		return $this->is_valid;
	}/**/
/*--------------------------------------------------------------------
VARS / MEMBERS
--------------------------------------------------------------------*/
	/**
	 * @var bool
	 */
	var $is_valid=true;
	
	/**
	* @var type script|function
	*/
	var $type;
	
	/**
	* @var string id
	*/
	//var $id;
	 
	/**
	 * @var webappkit parent's kit reference
	 */
	var $kit;

	/**
	 * @var string base file's filename
	 */
	var $file;
	
	/**
	* @var string class, if relevant
	*/
	var $event_class;

	/**
	* @var string function or method name, if relevant
	*/
	var $event_function;
/*--------------------------------------------------------------------
PRIVATE / PROTECTED METHODS
--------------------------------------------------------------------*/
	/**
	* runs a script event
	* @param httpRequestBean $request
	* @access protected
	* @return mixed
	*/
	function _runScript(&$request) {
		$_REQUEST=&$request;
		return include($this->file);
	}/**/
	
	/**
	* runs a function event
	* @param httpRequestBean $request
	* @access protected
	* @return mixed
	*/
	function _runFunction(&$request) {
		// loading function def if required
		//$function='event_'.$this->id;
		$function=$this->event_function;
		if (!function_exists($function)) {
			include($this->file);
		}
		return call_user_func($function,$request);
	}/**/

	/**
	*
	* @param
	* @access protected
	* @return
	*/
	function _runStaticMethod(&$request) {
		// loading class def if required
		if (!class_exists($this->event_class)) {
			include($this->file);
		}
		// calling method
		return call_user_func(array($this->event_class,$this->event_function),$request);
	}/**/

	/**
	*
	* @param
	* @access protected
	* @return
	* /
	function () {

	}/**/
}
?>